class ProgramsController < ApplicationController
  include ApplicationHelper
  
  before_filter :login_required, :except => [:index, :show, :search_tagged]
                :body_id
  
  def body_id
    @body_id = :program
  end
  
  # GET /programs/new
  def new
    @program = Program.new
  end
  
  # POST /programs
  # POST /programs.:format
  def create
    @program = Program.new(params[:program])
    
  	respond_to do |format|
      if @program.save
        flash[:notice] = "Success on creating the new program"
        format.html { redirect_to program_path(@program) }
        format.xml { head :ok }
  	  else
  	    flash[:error] = "Sorry, the program can't be created. Try again"
  	    format.html { render :action => 'new' }
  		  format.xml { head :xml => @program.errors.to_xml }
  	  end
  	end
  end
  
  # GET /programs
  # GET /programs.:format
  def index
    @programs = Program.find :all, :order => :name

    respond_to do |format|
      format.html
      format.xml { render :xml => @programs.to_xml(:except => [:updated_at]) }
    end
  end
  
  # GET /programs/:id
  # GET /programs/:id.:format
  def show
  	respond_to do |format|
      begin 
        @program = Program.find params[:id]
        @active_replication_systems = @program.find_where_program_is_replicated
        @inactive_replication_systems = @program.find_where_program_is_not_replicated
        @authors = @program.authors
        
    	  format.html { @tags = @program.tag_cloud }
    	  format.xml { render :xml => @program.to_xml( :include => [:broadcasts] ) }
    	rescue
        flash[:error] = "Sorry, the Program doesn't exist"
        format.html { redirect_to :action => 'index' }
        format.xml { head :ok }
      end
  	end
  end
  
  # PUT /programs/:id
  # PUT /programs/:id.:format
  def update
  	@program = Program.find params[:id]
  	
  	respond_to do |format|
  	  if @program.update_attributes params[:program]
  	    flash[:notice] = "Success on updating the program"
  	    format.html { redirect_to :action => 'show', :id => @program }
  		  format.xml { head :ok }
  	  else
  	    flash[:error] = "Sorry, the program can't be updated"
  	    format.html { render :action => 'edit' }
  		  format.xml { head :xml => @program.errors.to_xml }
  	  end
  	end
  end
  
  def edit
    @program = Program.find_by_id(params[:id])
  end  
  
  # GET /programs/authors/:id
  # GET /programs/authors/:id.:format
  def authors
    @program = Program.find(params[:id])

  	respond_to do |format|
  	  format.html { @users = User.find :all, :order => :name }
  	  format.xml { render :xml => @program.authors.to_xml( :root => 'authors' ) }
  	end
  end

  def tagging
    @program = Program.find params[:tagging][:resource_id]
    if @program
      @program.add_tags(params[:tagging][:tags])
      @program.save
      
      @tags = @program.tag_cloud
    else
      @tags = []
    end
    
    render :partial => 'shared/tag_cloud', :locals => { :tags => @tags, :controller => 'programs' }
  end

  def search_tagged
    @programs = Program.find_by_tags params[:tag]
    @all_tags = Program.tag_cloud
    @tag = params[:tag]
  end
  
  def begin_replication
    replication_system = ReplicaSystem.find params[:replica_system_id]
    program = Program.find params[:program_id]
    # Mark and save program in the replication system
    program.replica_systems << replication_system
    
    render :partial => 'replication', :locals => { :program => program, 
      :active_replication_systems => program.find_where_program_is_replicated, 
      :inactive_replication_systems => program.find_where_program_is_not_replicated }
  end
  
  def stop_replication
    replication_system = ReplicaSystem.find params[:replica_system_id]
    program = Program.find params[:program_id]
    # Mark and save program in the replication system
    program.replica_systems.delete replication_system
    
    render :partial => 'replication', :locals => { :program => program, 
      :active_replication_systems => program.find_where_program_is_replicated, 
      :inactive_replication_systems => program.find_where_program_is_not_replicated }
  end
end
